using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UKVSTS.SimpleImageEditControl;

namespace IEeee
{
    public partial class ImageModifier : Form
    {

        public ImageModifier(Image img)
        {
            InitializeComponent();

            editCtrl.Image = img;
            editCtrl.Mode = SimpleImageEditSurface.Mode.Cropping;
        }

        private void ImageModifier_Load(object sender, EventArgs e)
        {
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            // See if we have previously saved the location of this window.
            // This should stop people haveing to keep resizing the editor 
            // to their prefered size.
            //
            if (IEeeeConfig.Instance != null && IEeeeConfig.Instance.ImageEditorScreenCoords.IsEmpty == false)
            {
                // Ensure that it will fit on the screen!
                // Resoultions can change ...
                //
                if (Screen.GetWorkingArea(this).Contains(IEeeeConfig.Instance.ImageEditorScreenCoords))
                {
                    this.Location = IEeeeConfig.Instance.ImageEditorScreenCoords.Location;
                    this.Size = IEeeeConfig.Instance.ImageEditorScreenCoords.Size;
                }
            }
        }

        /// <summary>
        /// Returns the image that the user has created
        /// </summary>
        /// <returns></returns>
        public Image GetModifiedImage()
        {
            return editCtrl.GetModifiedImage();
        }

        /// <summary>
        /// Returns true if the user has made any modifications to the image.
        /// </summary>
        public bool HasImagedChanged
        {
            get { return editCtrl.HasImageChanged; }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (DialogResult == DialogResult.Cancel && editCtrl.HasImageChanged)
            {
                DialogResult res = MessageBox.Show(this, 
                                                    "Image has been changed, do you wish to keep these changes?",
                                                    "Warning", 
                                                    MessageBoxButtons.YesNoCancel,
                                                    MessageBoxIcon.Warning,
                                                    MessageBoxDefaultButton.Button3);
                
                if (res == DialogResult.Cancel) {
                    e.Cancel = true;
                }                
                else if (res == DialogResult.Yes)
                {
                    DialogResult = DialogResult.OK;
                }
            }

            try
            {
                Rectangle formLocation = new Rectangle(Location, Size);
                if (IEeeeConfig.Instance.ImageEditorScreenCoords != formLocation)
                {
                    IEeeeConfig.Instance.ImageEditorScreenCoords = formLocation;
                    IEeeeConfig.Instance.Save();
                }
            }
            catch (Exception) { }
        }

        private void ImageModifier_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            // Remove the "what is" cursor - arrow + question mark
            //
            e.Cancel = true;

            // Open up a browser and jump to our help page
            //
            System.Diagnostics.Process.Start("http://www.codeplex.com/UKVSTS/Wiki/View.aspx?title=Image%20Editor");
        }
    }
}